package com.mkz.jbpm.controller;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mkz.core.common.util.ResponseResult;
import com.mkz.core.log.annotation.SysLog;
import com.mkz.jbpm.entity.ActReDeployment;
import com.mkz.jbpm.service.ActReDeploymentService;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;


/**
 * 流程部署信息
 *
 * @author KuangJianyu
 * @date 2020-03-30 10:59:45
 */
@RestController
@AllArgsConstructor
@RequestMapping("/actredeployment" )
@Api(value = "actredeployment", tags = "流程部署信息管理")
public class ActReDeploymentController {
	private final RepositoryService repositoryService;
    private final  ActReDeploymentService actReDeploymentService;
    
    /**
     * 分页查询
     * @param page 分页对象
     * @param actReDeployment 流程部署信息
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public ResponseResult getActReDeploymentPage(Page page, ActReDeployment actReDeployment) {
        return ResponseResult.ok(actReDeploymentService.page(page, Wrappers.query(actReDeployment)));
    }

    /**
     * 部署新流程
     * @param actReDeployment 部署新流程
     * @return ResponseResult
     */
    @ApiOperation(value = "部署新流程", notes = "部署新流程")
    @SysLog("部署新流程" )
    @PostMapping("/deploy")
    @PreAuthorize("@pms.hasPermission('jbpm_re_deploy')" )
	public ResponseResult deploy(String deployName, String fileName, Authentication authentication) {
    	if(StrUtil.isBlank(deployName)) {
    		return ResponseResult.ok(false);
    	}
    	if(StrUtil.isBlank(fileName)) {
    		return ResponseResult.ok(false);
    	}
    	String fname = fileName.substring(0,fileName.lastIndexOf("."));
    	Deployment deployment = repositoryService.createDeployment()
    		.name(deployName)
    		.addClasspathResource("processes/"+fileName)
    		.deploy();
    	if(null==deployment||StrUtil.isBlank(deployment.getId())) {
    		return ResponseResult.ok(false);
    	}
        return ResponseResult.ok(true);
    }

    /**
     * 通过id查询流程部署信息
     * @param id id
     * @return ResponseResult
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public ResponseResult getById(@PathVariable("id" ) String id) {
        return ResponseResult.ok(actReDeploymentService.getById(id));
    }

    /**
     * 新增流程部署信息
     * @param actReDeployment 流程部署信息
     * @return ResponseResult
     */
    @ApiOperation(value = "新增流程部署信息", notes = "新增流程部署信息")
    @SysLog("新增流程部署信息" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('jbpm_actredeployment_add')" )
    public ResponseResult save(@RequestBody ActReDeployment actReDeployment) {
        return ResponseResult.ok(actReDeploymentService.save(actReDeployment));
    }

    /**
     * 修改流程部署信息
     * @param actReDeployment 流程部署信息
     * @return ResponseResult
     */
    @ApiOperation(value = "修改流程部署信息", notes = "修改流程部署信息")
    @SysLog("修改流程部署信息" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('jbpm_actredeployment_edit')" )
    public ResponseResult updateById(@RequestBody ActReDeployment actReDeployment) {
        return ResponseResult.ok(actReDeploymentService.updateById(actReDeployment));
    }

    /**
     * 通过id删除流程部署信息
     * @param id id
     * @return ResponseResult
     */
    @ApiOperation(value = "通过id删除流程部署信息", notes = "通过id删除流程部署信息")
    @SysLog("通过id删除流程部署信息" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('jbpm_actredeployment_del')" )
    public ResponseResult removeById(@PathVariable String id) {
        return ResponseResult.ok(actReDeploymentService.removeById(id));
    }
}
